using System;
using System.DHTML;
using Ext;

namespace SampleScripts.core {
    public class TemplateScript {
    	private Panel _p1;
		private Panel _p2;

        public static void main(Dictionary args) {
			ExtClass.onReady(new AnonymousDelegate(delegate() { new TemplateScript().init(); }));
        }

		public void init()
		{
			Dictionary data = new Dictionary(
				"name", "Jack Slocum",
				"company", "Ext JS, LLC",
				"address", "4 Red Bulls Drive",
				"city", "Cleveland",
				"state", "Ohio",
				"zip", "44102",
				"kids", new object[]
				        	{
				        		new Dictionary("name", "Sara Grace", "age", 3),
				        		new Dictionary("name", "Zachary", "age", 2),
				        		new Dictionary("name", "John James", "age", 0),
				        	}
				);

			_p1 = new Panel(new PanelConfig()
            	.title("Basic Template")
            	.width(300)
            	.html("<p><i>Apply the template to see results here</i></p>")
				.tbar(new object[]
                        {
                            new Ext.Toolbar.ButtonConfig()
                                .text("Apply Template")
                                .handler(new AnonymousDelegate(delegate
                                    {
                                        Template tpl = new Template(
                                            @"<p>Name: {name}</p>
											<p>Company: {company}</p>
											<p>Location: {city}, {state}</p>");
                                        Element body = (Element) Type.GetField(_p1, "body");
                                        tpl.overwrite(body, data);
                                        body.highlight("#c3daf9", new Dictionary("block", true));
                                    }))
								.ToDictionary()
                        })
            	.renderTo(Document.Body)
            	.ToDictionary());

            _p2 = new Panel(new PanelConfig()
                .title("XTemplate")
                .width(300)
                .html("<p><i>Apply the template to see results here</i></p>")
				.tbar(new object[]
                        {
                            new Ext.Toolbar.ButtonConfig()
                                .text("Apply Template")
			                    .handler(new AnonymousDelegate(delegate 
									{
										XTemplate tpl = new XTemplate(
											@"<p>Name: {name}</p>
											<p>Company: {company}</p>
											<p>Location: {city}, {state}</p>
											<p>Kids: 
											<tpl for=""kids"" if=""name=='Jack Slocum'"">
												<tpl if=""age &gt; 1""><p>{#}. {parent.name}'s kid - {name}</p></tpl>
											</tpl></p>");

										Element body = (Element)Type.GetField(_p2, "body");
										tpl.overwrite(body, data);
										body.highlight("#c3daf9", new Dictionary("block", true));
                                    }))
								.ToDictionary()
                        })
            	.renderTo(Document.Body)
            	.ToDictionary());
		}
    }
}
